package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {

    @Reference
    private RoleService roleService;

    /**
     * 查询所有角色
     */
    @RequestMapping("/findAll")
    public Result findAll(){
        try {
            List<Role> roleList = roleService.findAll();
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS,roleList);
        }catch (Exception ex){
            ex.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    /**
     * 根据用户所关联的角色id查询角色
     */
    @RequestMapping("/findRoleIdsByUserId")
    public Result findRoleIdsByUserId(Integer roleIds){
        List<Integer> ids = null;
        try {
            ids = roleService.findRoleIdsByUserId(roleIds);
        }catch (Exception ex){
            ex.printStackTrace();
            return new Result(true,MessageConstant.QUERY_ROLE_FAIL);
        }
        return new Result(true,MessageConstant.QUERY_ROLE_SUCCESS,ids);
    }

}
